package org.neuedu.servlet;

import org.neuedu.bean.Article;
import org.neuedu.bean.Category;
import org.neuedu.dao.UserDao;
import org.neuedu.dao.UserDaoImpl;
import org.neuedu.service.ArticleService;
import org.neuedu.service.ArticleServiceImpl;
import org.neuedu.bean.*;
import org.neuedu.service.UserService;
import org.neuedu.service.UserServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "ArticleAddServlet",urlPatterns = "/article/add")
public class ArticleAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取 class,title,content,experience,uid
        // 从session中获取登录者id
        // 准备dao和service dao : 1.插入文章信息；2.修改user表中的飞吻；3.更新session中对象的飞吻
        String aClass = request.getParameter("class");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String experience = request.getParameter("experience");
        HttpSession session=request.getSession();
        User user = (User)session.getAttribute("fly_user");
        Integer id = user.getId();
        Article article=new Article();
        article.setCid(Integer.valueOf(aClass));
        article.setTitle(title);
        article.setContent(content);
        article.setPayKiss(Integer.valueOf(experience));
        article.setUid(id);
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        article.setUpdateTime(ft.format(date));
        ArticleService articleService = new ArticleServiceImpl();
        articleService.saveArticleInfo(article);
        articleService.updateKissnum(Integer.valueOf(experience),id);
        response.sendRedirect(request.getContextPath()+"/index");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



    }
}
